const app = getApp()

// var baseURL = 'http://localhost:2999/'
// var baseURL = 'http://192.168.133.174:2999/'
var baseURL = 'http://ldjy.gnust.edu.cn/api/'
const Request = function request(option) {
    return new Promise(function (resolve, reject) {
        let header = {
            'content-type': 'application/json'
        };
        let token = wx.getStorageSync('token');
        if (token) {
            header.token = token;
        }
        wx.request({
            url: baseURL + option.url,
            method: option.method,
            data: option.data === undefined ? '' : JSON.stringify(option.data),
            header: header, // 请求头
            timeout: 10000, // 超时时间
            success(res) {
                console.log(res.header)
                if (res.header.Token) {
                    wx.setStorageSync('token', res.header.Token);
                }
                if(res.data.code === 401){
                    wx.navigateTo({
                        url: '../login/login',
                      })
                }
                resolve(res);
            },
            fail(err) {
        
                reject(err)
            }
        })
    })
}





module.exports = Request